package game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class LoadLevelPanel extends javax.swing.JPanel implements
		ActionListener {

	private int nrOfLevels;
	private Game frame;
	
	/**
	 * Creates the LoadLevelPanel
	 * adds as much Buttons as there are Levels in Game
	 */
	public LoadLevelPanel(Game pFrame) {

		nrOfLevels = Level.totalNrOfLevels;
		frame = pFrame;
		
		//ActionCmd is nr of level
		for (int i = 0; i < nrOfLevels; i++) {
			JButton newButton = new JButton("Load Level "
					+ String.valueOf(i));
			newButton.setActionCommand(String.valueOf(i));
			newButton.addActionListener(this);
			newButton.setToolTipText("Load Level " + String.valueOf(i));
			add(newButton);
		}
		JButton btn_back = new JButton ("BACK");
		btn_back.setToolTipText("Go back to Main Menu");
		btn_back.setActionCommand("back");
		btn_back.addActionListener(this);
		add(btn_back);
		this.setSize(500, 400);
	}

	/**
	 * load level determined by pressed button
	 * */
	public void actionPerformed(ActionEvent e) {
		
		for (int i = 0; i < nrOfLevels; i++) {
			if(e.getActionCommand().equals(String.valueOf(i))){
				 this.setVisible(false);
			        GamePanel gp = new GamePanel(i, 800, 600, frame);
			        gp.setPause(false);
			        
			        frame.add(gp);   
			        frame.remove(this);
			        frame.validate();
			}
			if(e.getActionCommand().equals("back")){
		        MenuPanel mp = new MenuPanel(frame);
		        
		        frame.add(mp);   
		        frame.remove(this);
		        frame.validate();
			}
		}
	}
}
